package com.ly.ocw.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SDKTestSendTemplateSMS {
	private static Logger logger = LoggerFactory.getLogger(SDKTestSendTemplateSMS.class);
	
	//验证码有效时间 5 分钟
	public static final String EFFECTIVE_TIME="15";
	
	//验证码位数
	public static final int NUM = 6;
	
	
	/**
	 * 6位随机数字
	 * @return
	 */
	public static String getVcode(){
		
		StringBuffer str = new StringBuffer();
		
		Random r =new Random();
		for (int i = 0; i < 6; i++) {
			int n =r.nextInt();
			
			n = (n<0?n*(-1):n)%10;
			
			str.append(n);
		}
		return str.toString();
	}
	
	/**
	 * 发送手机验证码
	 * @param to
	 * @param vcode
	 */
	public static void sendSms(String to,String vcode){
	/*<form action="http://www.mssms.cn:8000/msm/sdk/http/sendsmsutf8.jsp" id="inputForm" method="post" class="form-horizontal">
		<input type="text" id="year" name="username" value="JSMB260229"  />
		<input type="text" id="year" name="scode" value="096063" />
		<input type="text" id="year" name="mobile" value="13815864706" />
		<input type="text" id="year" name="content" value="@1@=123456" />
		<input type="text" id="year" name="tempid" value="MB-2013102300" />
		<input type="submit" class="btn green" value=" 查 询  "/>
	</form>*/
		
		CloseableHttpResponse response = null;
		String responseBody = "";
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpPost post = new HttpPost("http://www.mssms.cn:8000/msm/sdk/http/sendsmsutf8.jsp");
			
			List<NameValuePair> parameters =new ArrayList<NameValuePair>();
			parameters.add(new BasicNameValuePair("username", "JSMB260229"));
			parameters.add(new BasicNameValuePair("scode", "096063"));
			parameters.add(new BasicNameValuePair("mobile", to));
			parameters.add(new BasicNameValuePair("content", "@1@="+vcode));
			parameters.add(new BasicNameValuePair("tempid", "MB-2013102300"));
			
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters);
			post.setEntity(entity );
			
			logger.info("\n          发送短信："+vcode+" to:"+to);
			response = httpclient.execute(post);
			logger.info("\n          Executed response " +response.getStatusLine().toString());
			
			HttpEntity sespEntity = response.getEntity();
			if (sespEntity != null) {
				responseBody = EntityUtils.toString(sespEntity, "utf-8");
			}
		} catch (Exception e) {
			logger.error("发送短信失败："+vcode+" to:"+to);
		} finally {
			try {
				if(response!=null){
					response.close();	
				}
			} catch (IOException e) {
				logger.error("发送短信失败，关闭连接失败："+vcode+" to:"+to);
			}
		}
		logger.info("\n          responseBody----->>"+responseBody);
	}
	


}
